//
//  DYLunbotu.m
//  Craft
//
//  Created by BetaTheta on 16/3/7.
//  Copyright © 2016年 BetaTheta. All rights reserved.
//

#import "DYLunbotu.h"

@implementation DYLunbotu

#pragma mark - 字符串数组轮播图
- (instancetype)initWithFrame:(CGRect)frame stringArray:(NSMutableArray *)arr
{
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollView = [[UIScrollView alloc] init];
        [self addSubview:_scrollView];
        self.i = 0;
        
        _scrollView.frame = frame;
        // 设置大小
        _scrollView.contentSize = CGSizeMake(self.frame.size.width * arr.count, 0);
        // 设置代理
        _scrollView.delegate = self;
        // 整页滚
        _scrollView.pagingEnabled = YES;
        // 滚动条显/隐
        _scrollView.showsHorizontalScrollIndicator = NO;
        
        [arr addObject:arr.firstObject];
        
        // 轮播图添加图片
        for (NSInteger i = 0; i < arr.count; i++) {
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height)];
            [imgView sd_setImageWithURL:[NSURL URLWithString:arr[i]]placeholderImage:[UIImage imageNamed:@"placeholder1"]];
            [_scrollView addSubview:imgView];
        }
        // 创建pageControl
        self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 30, 200, 30)];
        self.page.center = CGPointMake(self.center.x, self.page.center.y);
        self.page.numberOfPages = arr.count - 1;
//        self.page.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.15];
        // 页码颜色
        _page.pageIndicatorTintColor = [UIColor colorWithWhite:0.902 alpha:1.000];
        // 当前页码颜色
        _page.currentPageIndicatorTintColor = [UIColor colorWithRed:1.000 green:0.400 blue:0.400 alpha:1.000];
        [self addSubview:self.page];
        
        // 记录数组个数
        self.arrCount = arr.count;
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(zidong) userInfo:nil repeats:YES];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        [_scrollView addGestureRecognizer:tap];
        
        BLOCK push = ^()
        {
            NSLog(@"跳转页面");
        };
        self.block_value = push;
    }
    return self;
}

#pragma mark - 自动播放装置 左向右滚
- (void)zidong
{
    // 因为有n张图 所以i值为0~n n个数
    if (_i < _arrCount) {
        // 让scroll 自己变化 带动画的变
        [self.scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width * _i , 0) animated:YES];
        // 判断是第几个图 是最后图的时候
        if (_i == _arrCount - 1) {
            // 最后一个的pageControl跳到第一个 因为有n个图片 但是只有n-1个点
            _page.currentPage = 0;
        } else {
            // 其他的时候 page的点 也自己随着i变化
            _page.currentPage = _i;
        }
        _i++;
        
    } else {
        // 超过了 就让i变回 从头开始
        _i = 0;
        _page.currentPage = _i;
        // 最后一张假图变回第一张图 不用动画
        [_scrollView setContentOffset:CGPointMake(0 , 0) animated:NO];
        
    }
    
    
}

// 滚动开始
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // 停止自动滚动
    [_timer invalidate];
    
    
}

// 滚动减速结束
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 匹配翻页页码
    
    if (_scrollView.contentOffset.x == _scrollView.frame.size.width * (_arrCount - 1)) {
        _scrollView.contentOffset = CGPointZero;
        _page.currentPage = 0;
    } else {
        _page.currentPage = _scrollView.contentOffset.x / _scrollView.frame.size.width;
    }
    
    _i = self.page.currentPage;
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(zidong) userInfo:nil repeats:YES];
    
}

- (void)tap:(UITapGestureRecognizer *)tap
{
    self.block_value();
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
